using System;
//using Microsoft.SPOT;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;

namespace EnecsysProxy
{
    public class PVOutput
    {
        internal string sKey;
        internal string sSid;
        
        public PVOutput(string Key, string Sid)
        {
            sKey = Key;
            sSid = Sid;
        }
        public void SendSolarOutput(double dPower, string sTemp, double dVoltage, int iPUsage = -1, double dHumidity = 0, double dPressure = 0)
        {
            try
            {

                string sDate = DateTime.Now.ToString("yyyyMMdd");
                string sTime = DateTime.Now.ToString("HH:mm");
                string sPower = System.Math.Round(dPower).ToString();
                string sParam = "&d=" + sDate + "&t=" + sTime + "&v2=" + sPower; //"&sid=" + sSid + "&key=" + sKey + 

                if (sTemp != "")
                    sParam = sParam + "&v5=" + sTemp;

                sParam = sParam + "&v6=" + System.Math.Round(dVoltage).ToString();
                if(iPUsage > -1)
                    sParam = sParam + "&v4=" + iPUsage.ToString();
                if(dHumidity > 0)
                    sParam = sParam + "&v7=" + dHumidity.ToString("N0").Replace(",","");
                if(dPressure > 0)
                    sParam = sParam + "&v8=" + dPressure.ToString("N2").Replace(",","");

				//Console.WriteLine(sParam);

                byte[] postBytes = Encoding.UTF8.GetBytes(sParam);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://pvoutput.org/service/r2/addstatus.jsp");
                webRequest.Timeout = 3000;
                webRequest.ReadWriteTimeout = 3000;
                webRequest.Method = "POST";
                webRequest.Headers.Add("X-Pvoutput-Apikey", sKey);
                webRequest.Headers.Add("X-Pvoutput-SystemId", sSid);
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.ContentLength = postBytes.Length;

                Stream postStream = webRequest.GetRequestStream();
                postStream.Write(postBytes, 0, postBytes.Length);
                postStream.Close();

                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                //Debug.Print(webResponse.StatusCode.ToString());
                //Debug.Print(webResponse.Server);

                Stream responseStream = webResponse.GetResponseStream();
                StreamReader responseStreamReader = new StreamReader(responseStream);

                string result = responseStreamReader.ReadToEnd();
                webResponse.Close();
                responseStream.Close();

                //Debug.Print("PVOut:" + result.ToString());

                //Thread.Sleep(100);

            }
            catch (Exception ex)
            {
				Console.WriteLine("E74: " + ex.Message.ToString());
            }
        }
    }
}
